# -*- coding: utf-8 -*-
import scrapy
# from scrapy import Request
from scrapy.selector import Selector
# from scrapy import FormRequest
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.http import Request,FormRequest
from zhihuspider.items import ZhihuspiderItem
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

class ZhihuSpider(CrawlSpider):
    name = "zhihu"
    allowed_domains = ["zhihu.com"]
    start_urls = ['https://www.zhihu.com/#signin']

    rules = (
        Rule(SgmlLinkExtractor(allow=('/question/\d+#.*?')),callback='parse_page',follow=True),
        Rule(SgmlLinkExtractor(allow=('/question/\d+')),callback='parse_page',follow=True)
    )

    cookies = {
        
    }

    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip,deflate",
        "Accept-Language": "en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4",
        "Connection": "keep-alive",
        "Content-Type": " application/x-www-form-urlencoded; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36",
        "Referer": "http://www.zhihu.com/"
    }

    def start_requests(self):
        print 'start .......'
        return [Request("https://www.zhihu.com/#signin",
                        meta={'cookiejar':1},
                callback=self.post_login)]

    def post_login(self, response):
        xsrf = Selector(response).xpath("//input[@name='_xsrf']/@value").extract()[0]
        print xsrf
        image = Selector(response).xpath("//div[@class='captcha-container']/img/@src").extract()

        return [Request("http://www.zhihu.com",
                                          cookies=,
                                          callback=self.after_login,
                                          meta ={'cookiejar':response.meta['cookiejar']},
                                          headers = self.headers,
                                          dont_filter = True
                                          )]

    def after_login(self,response):
        for url in self.start_urls:
            yield self.make_requests_from_url(url)

    def parse_page(self,response):
        problem = Selector(response)
        item = ZhihuspiderItem()
        item['url'] = response.url
        item['name'] = problem.xpath('//span[@class="name"]/text()').extract()
        print item['name']

